//
//  CJHomeViewController.swift
//  SwiftDemo
//
//  Created by Chris on 2017/6/12.
//  Copyright © 2017年 chris. All rights reserved.
//

import UIKit

class CJHomeViewController: CJBaseViewController {

    /// 首页视图模型
    fileprivate lazy var listModel = CJStatusListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    /// 加载数据
    override func loadData() {
        
        listModel.loadStatus (isPullUp: isPullUp) { (isSuccess, shouldRefresh) in
            
            print("数据加载结束")
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                self.refreshControl?.endRefreshing()
            })
            // 数据加载成功后, 恢复 isPullUp
            self.isPullUp = false
            if shouldRefresh {
                self.tableView?.reloadData()
            }
        }
    }
    
    /// 跳转好友界面
    @objc fileprivate func showFriends() {
        
    }
}

// MARK: - 设置界面
extension CJHomeViewController {
    
    /// 设置tableView
    override func setupTableView() {
        super.setupTableView()
        
        // 设置导航栏
        setupUserNaviTitle()
    }
    
    /// 设置导航栏用户名
    private func setupUserNaviTitle() {
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        // 用户名按钮
        let button = CJTitleButton(title: CJNetworkManager.sharedManager.userAccount.screen_name)
        
        button.addTarget(self, action: #selector(userTitleNameClick), for: .touchUpInside)
        navItem.titleView = button
    }
    
    /// 点击导航栏用户名
    @objc private func userTitleNameClick(btn: UIButton) {
        btn.isSelected = !btn.isSelected
    }
}

// MARK: - tableView 代理方法
extension CJHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        
        cell.textLabel?.text = listModel.statusList[indexPath.row].text
        return cell
    }
}
